﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyKhachSan.DataAccess;
using QuanLyKhachSan.BusinessObject;
using QuanLyKhachSan.BusinessLogic;
namespace QuanLyKhachSan
{
    public partial class frmPhieuThuePhong : Form
    {
        
        private PhieuThuePhongBL ptpBL = new PhieuThuePhongBL();
        private NhanVienBL nvBL = new NhanVienBL();
        private KhachHangBL khBL = new KhachHangBL();
        private PhongBL pBL = new PhongBL();
        private PhieuThuePhongBO ptpBO = new PhieuThuePhongBO();
        private string maPhong;
        private string maThuePhong;
        string maphong = null;
        public frmPhieuThuePhong()
        {
            InitializeComponent();
        }

        private void frmPhieuThuePhong_Load(object sender, EventArgs e)
        {
            //lấy mã nhân viết load ra tên NV
            colMaNhanVien.DataSource = nvBL.LayDanhSachNhanVien();
            colMaNhanVien.DisplayMember = "TenNhanVien";
            colMaNhanVien.ValueMember = "MaNhanVien";

            //lấy mã khách hàng load ra tên KH
            colMaKhachHang.DataSource = khBL.LayDSKhachHang();
            colMaKhachHang.DisplayMember = "TenKhachHang";
            colMaKhachHang.ValueMember = "MaKhachHang";

            //lấy mã phòng load ra tên phòng
            colMaPhong.DataSource = pBL.LayDSPhong();
            colMaPhong.DisplayMember = "TenPhong";
            colMaPhong.ValueMember = "MaPhong";

            //tình trạng phòng
            colTinhTrang.DataSource = ptpBL.LayTinhTrangThanhToan();
            colTinhTrang.DisplayMember = "TTThanhToan";
            colTinhTrang.ValueMember = "TinhTrang";

            //đổ dữ liệu ra datagridview
            dgvwPhieuThuePhong.DataSource = ptpBL.LayDSThuePhong();
        }

        private void dgvwPhieuThuePhong_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            ptpBO.MaThuePhong = dgvwPhieuThuePhong.Rows[dong].Cells["MaPhieuThuePhong"].Value.ToString();
            maThuePhong = dgvwPhieuThuePhong.Rows[dong].Cells["MaPhieuThuePhong"].Value.ToString();
            ptpBO.MaNhanVien = dgvwPhieuThuePhong.Rows[dong].Cells["colMaNhanVien"].Value.ToString();
            ptpBO.MaKhachHang = dgvwPhieuThuePhong.Rows[dong].Cells["colMaKhachHang"].Value.ToString();
            ptpBO.MaPhong = dgvwPhieuThuePhong.Rows[dong].Cells["colMaPhong"].Value.ToString();
            maPhong = dgvwPhieuThuePhong.Rows[dong].Cells["colMaPhong"].Value.ToString();
            ptpBO.NgayThue = Convert.ToDateTime(dgvwPhieuThuePhong.Rows[dong].Cells["NgayThue"].Value.ToString());
            ptpBO.NgayTra = Convert.ToDateTime(dgvwPhieuThuePhong.Rows[dong].Cells["NgayTra"].Value.ToString());
            ptpBO.TienPhong = int.Parse(dgvwPhieuThuePhong.Rows[dong].Cells["TienPhong"].Value.ToString());

        }

        private void LayDuLieu(PhieuThuePhongBO P)
        {
            this.maphong = P.MaPhong;
            if (!maphong.Equals(""))
            {
                dgvwPhieuThuePhong.DataSource = ptpBL.LayDSThuePhong();
            }
        }

        //tính tiền phòng
        private void btnTinhTienPhong_Click(object sender, EventArgs e)
        {
            int donGia = LoaiPhongBL.LayTien(PhongBL.LayMaLoaiPhong(maPhong));
            TimeSpan dt = new TimeSpan();
            dt = DateTime.Now - ptpBO.NgayThue;
            // MessageBox.Show(PhongBL.LayMaLoaiPhong(maPhong).ToString());
            int tong = dt.Days * donGia;
            txtTienPhong.Text = tong.ToString();
           
        }


        //tính tiền dịch vụ
        private void btnTinTienDV_Click(object sender, EventArgs e)
        {
            int tienDichVu = PhieuThuePhongBL.LayTienDichVu(ChiTietDVBL.LaymaDichVu(maThuePhong));
            txtTienDV.Text = tienDichVu.ToString();
        }

        //tính tổng tiền
        private void btnTongTien_Click(object sender, EventArgs e)
        {
            int tongTien = int.Parse(txtTienDV.Text) + int.Parse(txtTienPhong.Text);
            txtTongTien.Text = tongTien.ToString();
        }

        //nút thêm
        private void btnThem_Click(object sender, EventArgs e)
        {
            frmAddPhieuThuePhong faptp = new frmAddPhieuThuePhong();
            faptp.NextID();
            faptp.DuLieu = new frmAddPhieuThuePhong.DuLieuTruyenVe(LayDuLieu);
            faptp.Show();
        }

        //nút sửa 
        private void btnSua_Click(object sender, EventArgs e)
        {
            frmAddPhieuThuePhong faptp = new frmAddPhieuThuePhong();
            faptp.Load_Edit(ptpBO);
            faptp.DuLieu = new frmAddPhieuThuePhong.DuLieuTruyenVe(LayDuLieu);
            faptp.Show();
        }

        //nút xóa
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (ptpBO.MaThuePhong == "")
                MessageBox.Show("Chọn mã nhân viên", "Chương trình thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa hay không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    khBL.Delete(ptpBO.MaKhachHang);
                    frmPhieuThuePhong_Load(sender, e);
                }
            }
        }

        //nút thoát
        private void btnThoat_Click(object sender, EventArgs e)
        {
            bool kq = (MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes);
            if (kq == true)
            {
                this.Close();
            }
        }

        //tìm kiếm 
        private void txtTimKiem_KeyUp(object sender, KeyEventArgs e)
        {
            dgvwPhieuThuePhong.DataSource = ptpBL.TimKiemPhieuThuePhong(txtTimKiem.Text);
        }

        //lưu tiền phòng vào csdl
        private void btnLuu_Click(object sender, EventArgs e)
        {

            frmTinhTien.Tien = txtTongTien.Text;
            frmTinhTien ftt = new frmTinhTien();
            ftt.Load_TT(ptpBO);
            ftt.DuLieu = new frmTinhTien.DuLieuTruyenVe(LayDuLieu);
            ftt.Show();  
        }
    }
}
